<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${pathToMapperClass}">
    <resultMap id = "BaseResultMap" type= "${pathToModelClass}" >
        <id column="${primaryKey.colName}" jdbcType="${primaryKey.jdbcType}" property="${primaryKey.varName}"/>
    <#list otherField as item>
        <result column="${item.colName}" jdbcType="${item.jdbcType}" property="${item.varName}"/>
    </#list>
    </resultMap>
    <sql id="Base_Column_List">
        ${primaryKey.colName}<#list otherField as item>,${item.colName}</#list>
    </sql>

    <select id="selectAll" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${tableName}
    </select>

    <select id="getSelective" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${tableName}
        <where>
            <if test="${primaryKey.varName} != null">
                ${primaryKey.colName} =  ${r'#{'}${primaryKey.varName}${r'}'}
            </if>
            <#list otherField as item>
                <#if !(item.colName == "Fcreate_time" || item.colName == "Fmodify_time")>
                    <if test="${item.varName} != null ">
                        and ${item.colName} = ${r'#{'}${item.varName}${r'}'}
                    </if>
                </#if>
            </#list>
        </where>
    </select>

    <select id="selectWithPage" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${tableName}
        <where>
            <if test="${primaryKey.varName} != null">
                ${primaryKey.colName} =  ${r'#{'}${primaryKey.varName}${r'}'}
            </if>
            <#list otherField as item>
                <#if !(item.colName == "Fcreate_time" || item.colName == "Fmodify_time")>
                    <if test="${item.varName} != null ">
                        and ${item.colName} = ${r'#{'}${item.varName}${r'}'}
                    </if>
                </#if>
            </#list>
        </where>
    </select>

    <select id="getByPrimaryKey" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${tableName}
        where ${primaryKey.colName} = ${r'#{'}${primaryKey.varName}${r'}'}
    </select>

    <update id="update" parameterType="${pathToModelClass}">
        update ${tableName}
        <set>
    <#list otherField as item>
        <#if !(item.colName == "Fcreate_time" || item.colName == "Fmodify_time")>
            <if test="${item.varName} != null ">
                ${item.colName} = ${r'#{'}${item.varName}${r'}'},
            </if>
        </#if>
    </#list>
        </set>
        where ${primaryKey.colName} = ${r'#{'}${primaryKey.varName}${r'}'}
    </update>

    <insert id="insert" parameterType="${pathToModelClass}">
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list otherField  as item>
                <#if !(item.colName == "Fcreate_time" || item.colName == "Fmodify_time")>
                    <if test="${item.varName} != null ">
                        ${item.colName},
                    </if>
                </#if>
            </#list>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list otherField  as item>
                <#if !(item.colName == "Fcreate_time" || item.colName == "Fmodify_time")>
                    <if test="${item.varName} != null ">
                        ${r'#{'}${item.varName}${r'}'},
                    </if>
                </#if>
            </#list>
        </trim>
    </insert>

    <insert id="saveOrUpdate" parameterType="${pathToModelClass}">
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
    <#list otherField  as item>
        <#if !(item.colName == "Fcreate_time" || item.colName == "Fmodify_time")>
            <if test="${item.varName} != null ">
                ${item.colName},
            </if>
        </#if>
    </#list>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
    <#list otherField  as item>
        <#if !(item.colName == "Fcreate_time" || item.colName == "Fmodify_time")>
            <if test="${item.varName} != null ">
                ${r'#{'}${item.varName}${r'}'},
            </if>
        </#if>
    </#list>
        </trim>
        ON DUPLICATE KEY UPDATE
        <trim suffixOverrides=",">
    <#list otherField as item>
        <#if !(item.colName == "Fcreate_time" || item.colName == "Fmsodify_time")>
            <if test="${item.varName} != null ">
                ${item.colName} = ${r'#{'}${item.varName}${r'}'},
            </if>
        </#if>
    </#list>
        </trim>
    </insert>

<#function hasIsDelete fields>
    <#local  map ={} >
    <#list fields as item>
        <#if item.colName?contains("is_delete")>
            <#local map = map+{"flag":true}>
            <#local map = map+{"colName":item.colName}>
            <#return map/>
        </#if>
    </#list>
    <#return {"flag":false}>
</#function>
    <delete id="delete">
    <#assign  result = hasIsDelete(otherField)/>
    <#if result.flag>
        update ${tableName}
        set ${result.colName} = 1
        where ${primaryKey.colName} = ${r'#{'}${primaryKey.varName}${r'}'}
    <#else>
        delete from ${tableName}
        where ${primaryKey.colName} = ${r'#{'}${primaryKey.varName}${r'}'}
    </#if>
    </delete>
</mapper>